package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AvatarCollectAdapter;
import flc.ast.adapter.WallpaperCollectAdapter;
import flc.ast.databinding.ActivityCollectBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.bean.StkResBean;

public class CollectActivity extends BaseAc<ActivityCollectBinding> {

    private List<StkResBean> listShow = new ArrayList<>();
    private AvatarCollectAdapter avatarCollectAdapter;
    private WallpaperCollectAdapter wallpaperCollectAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCollectBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        mDataBinding.rvAvatarList.setLayoutManager(gridLayoutManager);
        avatarCollectAdapter = new AvatarCollectAdapter();
        mDataBinding.rvAvatarList.setAdapter(avatarCollectAdapter);
        avatarCollectAdapter.setGridLayoutManager(gridLayoutManager);
        avatarCollectAdapter.addChildClickViewIds(R.id.ivAvatarItemCollect);
        avatarCollectAdapter.setOnItemChildClickListener(this);

        mDataBinding.rvWallpaperList.setLayoutManager(new GridLayoutManager(mContext, 2));
        wallpaperCollectAdapter = new WallpaperCollectAdapter();
        mDataBinding.rvWallpaperList.setAdapter(wallpaperCollectAdapter);
        wallpaperCollectAdapter.addChildClickViewIds(R.id.ivWallpaperItemCollect);
        wallpaperCollectAdapter.setOnItemChildClickListener(this);

        mDataBinding.tvAvatar.setOnClickListener(this);
        mDataBinding.tvWallpaper.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvAvatar://头像
                clearSelection();
                getAvatarData();
                mDataBinding.tvAvatar.setBackgroundResource(R.drawable.bt_kp);
                break;
            case R.id.tvWallpaper://壁纸
                clearSelection();
                getWallpaperData();
                mDataBinding.tvWallpaper.setBackgroundResource(R.drawable.bt_kp);
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (adapter == avatarCollectAdapter) {
            switch (view.getId()) {
                case R.id.ivAvatarItemCollect:
                    List<StkResBean> listSp = AppSpUtils.getAvatarCollectList();
                    listSp.remove(avatarCollectAdapter.getItem(position));
                    AppSpUtils.saveAvatarCollectList(listSp);
                    getAvatarData();
                    break;
            }
        } else if (adapter == wallpaperCollectAdapter) {
            switch (view.getId()) {
                case R.id.ivWallpaperItemCollect:
                    List<StkResBean> listSp = AppSpUtils.getWallpaperCollectList();
                    listSp.remove(wallpaperCollectAdapter.getItem(position));
                    AppSpUtils.savWallpaperCollectList(listSp);
                    getWallpaperData();
                    break;
            }
        }
    }

    @Override
    protected void initData() {
        getAvatarData();
    }

    private void getAvatarData() {
        listShow.clear();
        List<StkResBean> listSp = AppSpUtils.getAvatarCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            avatarCollectAdapter.setList(listShow);
            mDataBinding.rvAvatarList.setVisibility(View.VISIBLE);
            mDataBinding.rvWallpaperList.setVisibility(View.GONE);
            mDataBinding.ivCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAvatarList.setVisibility(View.GONE);
            mDataBinding.rvWallpaperList.setVisibility(View.GONE);
            mDataBinding.ivCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getWallpaperData() {
        listShow.clear();
        List<StkResBean> listSp = AppSpUtils.getWallpaperCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            wallpaperCollectAdapter.setList(listShow);
            mDataBinding.rvAvatarList.setVisibility(View.GONE);
            mDataBinding.rvWallpaperList.setVisibility(View.VISIBLE);
            mDataBinding.ivCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAvatarList.setVisibility(View.GONE);
            mDataBinding.rvWallpaperList.setVisibility(View.GONE);
            mDataBinding.ivCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    private void clearSelection() {
        mDataBinding.tvAvatar.setBackgroundResource(R.drawable.bt_kp2);
        mDataBinding.tvWallpaper.setBackgroundResource(R.drawable.bt_kp2);
    }

}